package io.modifier.basic.emo.photo.data

import androidx.annotation.MainThread

internal object PhotoClipperDelivery {
    private val dataMap = mutableMapOf<Long, PhotoProvider>()

    @MainThread
    fun put(photoProvider: PhotoProvider): Long {
        val id = System.currentTimeMillis()
        dataMap[id] = photoProvider
        // memory leak protection
        val iterator = dataMap.iterator()
        while (iterator.hasNext()) {
            val next = iterator.next()
            if (next.key < id - 20 * 1000) {
                iterator.remove()
            }
        }
        return id
    }

    @MainThread
    fun peek(id: Long): PhotoProvider? {
        return dataMap[id]
    }

    @MainThread
    fun getAndRemove(id: Long): PhotoProvider? {
        return dataMap.remove(id)
    }

    @MainThread
    fun remove(id: Long) {
        dataMap.remove(id)
    }
}
